package cn.com.checne.controller.superManager;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.com.checne.util.*;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import cn.com.checne.constants.Constants;
import cn.com.checne.controller.BaseController;
import cn.com.checne.dao.LogInfoDao;
import cn.com.checne.dao.PerformanceCategoryDao;
import cn.com.checne.dao.PerformanceInfoDao;
import cn.com.checne.model.domain.PerformanceCategory;
import cn.com.checne.model.domain.PerformanceInfo;
import cn.com.checne.model.dto.PerformanceQuery;
import cn.com.checne.util.images.ImageContentUtils;

@Controller
@RequestMapping(value="/super/performance")
public class PerformanceInfoManagerController extends BaseController {
	protected static final Logger logger = Logger.getLogger(PerformanceInfoManagerController.class);

	@Resource
	private PerformanceCategoryDao cateDao;
	@Resource
	private PerformanceInfoDao performanceDao;
	@Resource
	private LogInfoDao logDao;
	@Resource
	private List<String> allowedUploadImages ;
	
	//公司业绩--首页
	@RequestMapping(value="/index.do")
	public String index(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		return "super/frame/frame_performance";
	}
	
	//公司业绩--左侧导航
	@RequestMapping(value="/left.do")
	public String left(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		return "super/performance/left";
	}
	
	//公司业绩--列举所有的栏目
	@RequestMapping(value="/category-list.do")
	public ModelAndView categoryList(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		int languageId = (String)request.getSession().getAttribute(Constants.SESSIONLANGUAGE) != null ? Integer.parseInt((String)request.getSession().getAttribute(Constants.SESSIONLANGUAGE)) : 1;
		List<PerformanceCategory> cateList = cateDao.getAll(languageId);
		modelMap.put("cateList", cateList);
		modelMap.put("languageId", languageId);
		mav.setViewName("super/performance/category-list");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//公司业绩--栏目新增、修改页面
	@RequestMapping(value="/category-show.do")
	public ModelAndView categoryShow(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		int cateId = WebUtils.getRequestParameterAsInt(request, "cateId", 0);
		int languageId = (String)request.getSession().getAttribute(Constants.SESSIONLANGUAGE) != null ? Integer.parseInt((String)request.getSession().getAttribute(Constants.SESSIONLANGUAGE)) : 1;
		PerformanceCategory cate = new PerformanceCategory();
		cate.setLanguageId(languageId);
		String opt = "create" ;
		if(cateId > 0){
			cate = cateDao.getPerformanceCategory(cateId);
			opt = "update" ;
		}
		modelMap.put("cate", cate);
		modelMap.put("opt", opt);
		mav.setViewName("super/performance/category-show");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//公司业绩--栏目新增
	@RequestMapping(value="/category-create.do")
	public ModelAndView categoryCreate(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		String message = "" ;
		PerformanceCategory cate = new PerformanceCategory();
		cate.setLanguageId(WebUtils.getRequestParameterAsInt(request, "languageId",1));
		cate.setShowType(WebUtils.getRequestParameterAsInt(request, "showType",0));
		cate.setName(WebUtils.getRequestParameterAsString(request, "name"));
		cate.setDesc(WebUtils.getRequestParameterAsString(request, "desc"));
		cate.setOrder(WebUtils.getRequestParameterAsInt(request, "order",0));
		if(cateDao.create(cate)){
			message = "公司业绩栏目创建成功!" ;
		}else{
			message = "公司业绩栏目创建失败!" ;
		}
		modelMap.put("message", message);
		modelMap.put("href", "super/performance/category-list.do");
		mav.setViewName("common/message");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//公司业绩--栏目修改
	@RequestMapping(value="/category-update.do")
	public ModelAndView categoryUpdate(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		PerformanceCategory cate = new PerformanceCategory();
		int cateId = WebUtils.getRequestParameterAsInt(request, "cateId", 0);
		cate = cateDao.getPerformanceCategory(cateId);
		cate.setLanguageId(WebUtils.getRequestParameterAsInt(request, "languageId",1));
		cate.setShowType(WebUtils.getRequestParameterAsInt(request, "showType",0));
		cate.setName(WebUtils.getRequestParameterAsString(request, "name"));
		cate.setDesc(WebUtils.getRequestParameterAsString(request, "desc"));
		cate.setOrder(WebUtils.getRequestParameterAsInt(request, "order",0));
		if(cateDao.update(cate)){
			message = "公司业绩栏目修改成功!" ;
		}else{
			message = "公司业绩栏目修改失败!" ;
		}
		modelMap.put("message", message);
		modelMap.put("href", "super/performance/category-list.do");
		mav.setViewName("common/message");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//公司业绩--栏目删除
	@RequestMapping(value="/category-del.do")
	public ModelAndView categoryDel(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		int cateId = WebUtils.getRequestParameterAsInt(request, "cateId", 0);
		if(cateDao.delete(cateId)){
			message = "公司业绩栏目删除成功!" ;
		}else{
			message = "公司业绩栏目删除失败!" ;
		}
		modelMap.put("message", message);
		modelMap.put("href", "super/performance/category-list.do");
		mav.setViewName("common/message");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//公司业绩--列举所有的栏目
	@RequestMapping(value="/performance-list.do")
	public ModelAndView PerformanceList(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		int languageId = (String)request.getSession().getAttribute(Constants.SESSIONLANGUAGE) != null ? Integer.parseInt((String)request.getSession().getAttribute(Constants.SESSIONLANGUAGE)) : 1;
		List<PerformanceCategory> cateList = cateDao.getAll(languageId);
		
		String kw = WebUtils.getRequestParameterAsString(request, "kw");
		kw = StringTools.converISO2UTF8(kw);
		int cateId = WebUtils.getRequestParameterAsInt(request, "cateId", 0);
		int isShowIndex = WebUtils.getRequestParameterAsInt(request, "isShowIndex", -1);
		int isShowList = WebUtils.getRequestParameterAsInt(request, "isShowList", -1);
		int isTop = WebUtils.getRequestParameterAsInt(request, "isTop", -1);
		int order = WebUtils.getRequestParameterAsInt(request, "order",0);
		String startTime = WebUtils.getRequestParameterAsString(request, "startTime");
		String endTime = WebUtils.getRequestParameterAsString(request, "endTime");
		
		/*if(StringUtils.isNotBlank(startTime)){
			startTime = TimeUtils.getDate(new Date(), "yyyy-MM-dd HH:mm:ss", Constants.SEARCH_DAYS);
		}
		if(StringUtils.isNotBlank(endTime)){
			endTime = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		}*/
		
		PerformanceQuery query = new PerformanceQuery();
		query.setKw(kw);
		query.setLanguageId(languageId);
		query.setCateId(cateId);
		query.setIsShowIndex(isShowIndex);
		query.setIsShowList(isShowList);
		query.setIsTop(isTop);
		query.setOrder(order);
		query.setStartTime(startTime);
		query.setEndTime(endTime);
		
	    int curPage = WebUtils.getRequestParameterAsInt(request, "curPage", 1);
	    PageBean pageBean = new PageBean();
	    pageBean.setCurPage(curPage);
	    List<PerformanceInfo> performanceList = performanceDao.getPerformanceListByCondition(query, pageBean.getStart(), pageBean.getRowsPerPage());
	    pageBean.setMaxRowCount(performanceDao.getPerformanceCountByCondition(query));
	    pageBean.setMaxPage();
	    pageBean.setPageNoList();
	    
		modelMap.put("cateList", cateList);
		modelMap.put("performanceList", performanceList);
		modelMap.put("query", query);
		modelMap.put("pageBean", pageBean);
		mav.setViewName("super/performance/performance-list");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//公司业绩--公司业绩新增、修改页面
	@RequestMapping(value="/performance-show.do")
	public ModelAndView PerformanceShow(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		int PerformanceId = WebUtils.getRequestParameterAsInt(request, "performanceId", 0);
		int languageId = (String)request.getSession().getAttribute(Constants.SESSIONLANGUAGE) != null ? Integer.parseInt((String)request.getSession().getAttribute(Constants.SESSIONLANGUAGE)) : 1;
		List<PerformanceCategory> cateList = cateDao.getAll(languageId);
		PerformanceInfo performance = new PerformanceInfo();
		performance.setLanguageId(languageId);
		String opt = "create" ;
		if(PerformanceId > 0){
			performance = performanceDao.getPerformanceById(PerformanceId);
			String content = performance.getContent();
			performance.setContent(ImageContentUtils.setBasePathToImagePath(request, content));
			opt = "update" ;
		}
		modelMap.put("cateList", cateList);
		modelMap.put("opt", opt);
		modelMap.put("performance", performance);
        modelMap.put("languageId", languageId);
		mav.setViewName("super/performance/performance-show");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//公司业绩--公司业绩新增
	@SuppressWarnings("deprecation")
	@RequestMapping(value="/performance-create.do")
	public ModelAndView PerformanceCreate(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("common/message");
		PerformanceInfo Performance = new PerformanceInfo();
		String content = WebUtils.getRequestParameterAsString(request, "content") ;
		String textContent = WebUtils.getRequestParameterAsString(request, "textContent");
        int contentType = WebUtils.getRequestParameterAsInt(request, "contentType", 1);
        if ((StringUtils.isBlank(content) && contentType == 1)  || (StringUtils.isBlank(textContent)) && contentType == 0) {
        	modelMap.put("message", "内容不能为空!");
			mav.addAllObjects(modelMap);
			return mav ;
		}
        
		if(contentType == 0 ){
        	Performance.setContent(textContent);
        }
        else{
    		content = ImageContentUtils.removeImageBasePathForString(content, request);
        }
        
        
		
		// 图片上传
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		MultipartFile multipartFile = multipartRequest.getFile("image");
		String originalFilename = multipartFile.getOriginalFilename();
		if(StringUtils.isNotBlank(originalFilename)){
			if (!isAllowed(originalFilename)) {
				modelMap.put("message", "上传文件类型不符合,必须是以下几种<br/>"
						+ this.allowedUploadImages.toString());
				mav.addAllObjects(modelMap);
				return mav ;
			}

			String fileName = DateFormatUtils.format(new Date(), "yyyyMMddHHmmss")
					+ "." + FilenameUtils.getExtension(originalFilename);
			String basePath = request.getRealPath("/") + "/";
			String relativePath = "userfiles/upload/user-images/"
					+ DateFormatUtils.format(new Date(), "yyyy-MM-dd").replaceAll(
							"-", "/") + "/";
			FileUtils.mkdirs(basePath + relativePath);
			multipartFile.transferTo(new File(basePath + relativePath + fileName));
			Performance.setFirstImage(relativePath + fileName);
		}
		
		int languageId = WebUtils.getRequestParameterAsInt(request, "languageId",1);
		int cateId = WebUtils.getRequestParameterAsInt(request, "cateId",0);
		
		boolean createFlag = true ;
		//首先判断选定的栏目是否支持列表、详情显示。如果是详情显示的话，需要进行更新操作。
		PerformanceInfo dbPerformance = performanceDao.isPerformanceExist(languageId,cateId);
		if(dbPerformance != null && dbPerformance.getPerformanceCategory().getShowType() == 0){
			createFlag = false ;
			Performance = performanceDao.getPerformanceById(dbPerformance.getPerformanceId());
		}
		
		Performance.setLanguageId(languageId);
		Performance.setCateId(cateId);
		Performance.setTitle(WebUtils.getRequestParameterAsString(request, "title"));
		Performance.setAuthor(WebUtils.getRequestParameterAsString(request, "title"));
		Performance.setPublishFrom(WebUtils.getRequestParameterAsString(request, "publishFrom"));
		Performance.setAuthor(WebUtils.getRequestParameterAsString(request, "author"));
		Performance.setPublishDate(new Date());
		Performance.setMetaKeyword(WebUtils.getRequestParameterAsString(request, "metaKeyword"));
		Performance.setMetaContent(WebUtils.getRequestParameterAsString(request, "metaContent"));
		Performance.setSummary(Performance.getMetaContent());
		Performance.setContent(content);
		Performance.setContentType(contentType);
		
		if(createFlag){
			if(performanceDao.create(Performance)){
				message = "公司业绩创建成功!" ;
			}else{
				message = "企业创建失败!" ;
			}
		}else{
			if(performanceDao.update(Performance)){
				message = "企业更新成功!" ;
			}else{
				message = "企业更新失败!" ;
			}
		}
		modelMap.put("message", message);
		modelMap.put("href", "super/performance/performance-list.do");
		mav.setViewName("common/message");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//公司业绩--公司业绩修改
	@SuppressWarnings("deprecation")
	@RequestMapping(value="/performance-update.do")
	public ModelAndView PerformanceUpdate(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		String message = "" ;
		String content = WebUtils.getRequestParameterAsString(request, "content") ;
		if(StringUtils.isBlank(content)){
			modelMap.put("message", "内容不能为空!");
			mav.addAllObjects(modelMap);
			return mav ;
		}
		
		content = ImageContentUtils.removeImageBasePathForString(content, request);
		
		int PerformanceId = WebUtils.getRequestParameterAsInt(request, "performanceId", 0);
		PerformanceInfo Performance = new PerformanceInfo();
		Performance = performanceDao.getPerformanceById(PerformanceId);
		
		// 图片上传
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		MultipartFile multipartFile = multipartRequest.getFile("image");
		if(multipartFile != null){
			String originalFilename = multipartFile.getOriginalFilename();
			if(StringUtils.isNotBlank(originalFilename)){
				if (!isAllowed(originalFilename)) {
					modelMap.put("message", "上传文件类型不符合,必须是以下几种<br/>"
							+ this.allowedUploadImages.toString());
					mav.addAllObjects(modelMap);
					return mav ;
				}
	
				String fileName = DateFormatUtils.format(new Date(), "yyyyMMddHHmmss")
						+ "." + FilenameUtils.getExtension(originalFilename);
				String basePath = request.getRealPath("/") + "/";
				String relativePath = "userfiles/upload/user-images/"
						+ DateFormatUtils.format(new Date(), "yyyy-MM-dd").replaceAll(
								"-", "/") + "/";
				FileUtils.mkdirs(basePath + relativePath);
				multipartFile.transferTo(new File(basePath + relativePath + fileName));
				Performance.setFirstImage(relativePath + fileName);
			}
		}
		
		
		Performance.setLanguageId(WebUtils.getRequestParameterAsInt(request, "languageId",1));
		Performance.setCateId(WebUtils.getRequestParameterAsInt(request, "cateId",0));
		Performance.setTitle(WebUtils.getRequestParameterAsString(request, "title"));
		Performance.setAuthor(WebUtils.getRequestParameterAsString(request, "title"));
		Performance.setPublishFrom(WebUtils.getRequestParameterAsString(request, "publishFrom"));
		Performance.setAuthor(WebUtils.getRequestParameterAsString(request, "author"));
        String publishDate = WebUtils.getRequestParameterAsString(request, "publishDate");
        Performance.setPublishDate(StringUtils.isBlank(publishDate) ? new Date() : TimeUtils.formatDate(publishDate));
        Performance.setMetaKeyword(WebUtils.getRequestParameterAsString(request, "metaKeyword"));
		Performance.setMetaContent(WebUtils.getRequestParameterAsString(request, "metaContent"));
		Performance.setSummary(Performance.getMetaContent());
		
		Performance.setContent(content);
		
		if(performanceDao.update(Performance)){
			message = "公司业绩修改成功!" ;
		}else{
			message = "公司业绩修改失败!" ;
		}
		modelMap.put("message", message);
		modelMap.put("href", "super/performance/performance-list.do");
		mav.setViewName("common/message");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//公司业绩--公司业绩删除
	@RequestMapping(value="/performance-del.do")
	public ModelAndView PerformanceDel(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		String message = "公司业绩删除成功!" ;
		String performanceId = WebUtils.getRequestParameterAsString(request, "performanceId");
		String ids [] = StringUtils.split(performanceId, ",") ;
		boolean flag = false ;
		for(String id : ids){
			flag = performanceDao.delete(Integer.parseInt(id));
			if(flag == false){
				message = "公司业绩删除失败!" ;
				break ;
			}
		}
		modelMap.put("message", message);
		modelMap.put("href", "super/performance/performance-list.do");
		mav.setViewName("common/message");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//公司业绩--首页显示设置
	@RequestMapping(value="/performance-setIndex.do")
	public ModelAndView PerformanceSetIndex(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		String message = "" ;
		String PerformanceId = WebUtils.getRequestParameterAsString(request, "performanceId");
		String flag = WebUtils.getRequestParameterAsString(request, "flag");
		if(StringUtils.isNotBlank(flag) && flag.matches("\\d+")){
			String nds [] = StringUtils.split(PerformanceId, ",") ;
			int PerformanceIds [] = new int[nds.length] ;
			int i = 0 ;
			for(String nd : nds){
				PerformanceIds[i] = Integer.valueOf(nd);
				i ++ ;
			}
			if(performanceDao.setShowIndex(Integer.valueOf(flag),PerformanceIds)){
				message = "公司业绩" + (flag.equals("1") ? "设置首页显示" : "取消首页显示") + "成功!" ;
			}else{
				message = "公司业绩" + (flag.equals("1") ? "设置首页显示" : "取消首页显示") + "失败!" ;
			}
		}
		
		modelMap.put("message", message);
		modelMap.put("href", "super/performance/performance-list.do");
		mav.setViewName("common/message");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	//公司业绩--列表页显示设置
	@RequestMapping(value="/performance-setList.do")
	public ModelAndView PerformanceSetList(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		String message = "" ;
		String PerformanceId = WebUtils.getRequestParameterAsString(request, "performanceId");
		String flag = WebUtils.getRequestParameterAsString(request, "flag");
		if(StringUtils.isNotBlank(flag) && flag.matches("\\d+")){
			String nds [] = StringUtils.split(PerformanceId, ",") ;
			int PerformanceIds [] = new int[nds.length] ;
			int i = 0 ;
			for(String nd : nds){
				PerformanceIds[i] = Integer.valueOf(nd);
				i ++ ;
			}
			if(performanceDao.setShowList(Integer.valueOf(flag),PerformanceIds)){
				message = "公司业绩" + (flag.equals("1") ? "设置列表页显示" : "取消列表页显示") + "成功!" ;
			}else{
				message = "公司业绩" + (flag.equals("1") ? "设置列表页显示" : "取消列表页显示") + "失败!" ;
			}
		}
		
		modelMap.put("message", message);
		mav.setViewName("common/message");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	public boolean isAllowed(String fileName) {
		for (String suffix : this.allowedUploadImages) {
			if (StringUtils.equalsIgnoreCase(suffix,
					FilenameUtils.getExtension(fileName))) {
				return true;
			}
		}
		return false;
	}
	
	
	//公司业绩--删除图片
	@RequestMapping(value="/performance-delImage.do")
	public void PerformanceDelImage(HttpServletRequest request,HttpServletResponse response) throws Exception {
		int PerformanceId = WebUtils.getRequestParameterAsInt(request, "performanceId",0);
		if(PerformanceId > 0 ){
			PerformanceInfo p = new PerformanceInfo();
			p = performanceDao.getPerformanceById(PerformanceId);
			p.setFirstImage("");
			performanceDao.update(p);
		}
	}
	
	@RequestMapping(value="/performance-sort.do",method=RequestMethod.GET)
	@Deprecated
	//暂时废弃掉
	public ModelAndView PerformanceSort(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		int languageId = (String)request.getSession().getAttribute(Constants.SESSIONLANGUAGE) != null ? Integer.parseInt((String)request.getSession().getAttribute(Constants.SESSIONLANGUAGE)) : 1;
		List<PerformanceCategory> cateList = cateDao.getAll(languageId);
		
		String kw = WebUtils.getRequestParameterAsString(request, "kw");
		kw = StringTools.converISO2UTF8(kw);
		int cateId = WebUtils.getRequestParameterAsInt(request, "cateId", 0);
		int isShowIndex = WebUtils.getRequestParameterAsInt(request, "isShowIndex", -1);
		int isShowList = WebUtils.getRequestParameterAsInt(request, "isShowList", -1);
		int isTop = WebUtils.getRequestParameterAsInt(request, "isTop", -1);
		//int order = WebUtils.getRequestParameterAsInt(request, "order",0);
		String startTime = WebUtils.getRequestParameterAsString(request, "startTime");
		String endTime = WebUtils.getRequestParameterAsString(request, "endTime");
		
		/*if(StringUtils.isBlank(startTime)){
			startTime = TimeUtils.getDate(new Date(), "yyyy-MM-dd HH:mm:ss", Constants.SEARCH_DAYS);
		}
		if(StringUtils.isBlank(endTime)){
			endTime = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		}*/
		
		//排序开始
		String flag = WebUtils.getRequestParameterAsString(request, "flag");
		//获取当前文章的排序ID
		int orderId = WebUtils.getRequestParameterAsInt(request, "orderId", 0);
		int performanceId = WebUtils.getRequestParameterAsInt(request, "performanceId", 0);
		performanceDao.sort(performanceId, orderId,flag);
		//排序开结束

		PerformanceQuery query = new PerformanceQuery();
		query.setKw(kw);
		query.setLanguageId(languageId);
		query.setCateId(cateId);
		query.setIsShowIndex(isShowIndex);
		query.setIsShowList(isShowList);
		query.setIsTop(isTop);
		query.setOrder(1);
		query.setStartTime(startTime);
		query.setEndTime(endTime);
		
	    int curPage = WebUtils.getRequestParameterAsInt(request, "curPage", 1);
	    PageBean pageBean = new PageBean();
	    pageBean.setCurPage(curPage);
	    List<PerformanceInfo> performanceList = performanceDao.getPerformanceListByCondition(query, pageBean.getStart(), pageBean.getRowsPerPage());
	    pageBean.setMaxRowCount(performanceDao.getPerformanceCountByCondition(query));
	    pageBean.setMaxPage();
	    pageBean.setPageNoList();
	    
		modelMap.put("cateList", cateList);
		modelMap.put("performanceList", performanceList);
		modelMap.put("query", query);
		modelMap.put("pageBean", pageBean);
		mav.setViewName("super/performance/performance-list");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	@RequestMapping(value="/performance-setPerformanceOrder.do",method=RequestMethod.GET)
	public ModelAndView setPerformanceOrder(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		ModelAndView mav = new ModelAndView();
		int languageId = (String)request.getSession().getAttribute(Constants.SESSIONLANGUAGE) != null ? Integer.parseInt((String)request.getSession().getAttribute(Constants.SESSIONLANGUAGE)) : 1;
		List<PerformanceCategory> cateList = cateDao.getAll(languageId);
		
		String kw = WebUtils.getRequestParameterAsString(request, "kw");
		kw = StringTools.converISO2UTF8(kw);
		int cateId = WebUtils.getRequestParameterAsInt(request, "cateId", 0);
		int isShowIndex = WebUtils.getRequestParameterAsInt(request, "isShowIndex", -1);
		int isShowList = WebUtils.getRequestParameterAsInt(request, "isShowList", -1);
		int isTop = WebUtils.getRequestParameterAsInt(request, "isTop", -1);
		String startTime = WebUtils.getRequestParameterAsString(request, "startTime");
		String endTime = WebUtils.getRequestParameterAsString(request, "endTime");
		
		String orderIds = WebUtils.getRequestParameterAsString(request, "orderIds","");
		String noIds = WebUtils.getRequestParameterAsString(request, "noIds","");
		String orderIdArray [] = StringUtils.split(orderIds, ",");
		String noIdArray [] = StringUtils.split(noIds, ",");
		
		if(!isNumeric(orderIdArray) || !isNumeric(noIdArray)){
			modelMap.put("message", "排序必须输入\"数字\"");
			mav.setViewName("common/message");
			mav.addAllObjects(modelMap);
			return mav ;
		}
		
		
		if(orderIdArray.length != noIdArray.length){
			modelMap.put("message", "不允许为空");
			mav.setViewName("common/message");
			mav.addAllObjects(modelMap);
			return mav ;
		}
		
		//排序开始
		performanceDao.setPerformanceOrder(orderIdArray, noIdArray);
		//排序开结束

		PerformanceQuery query = new PerformanceQuery();
		query.setKw(kw);
		query.setLanguageId(languageId);
		query.setCateId(cateId);
		query.setIsShowIndex(isShowIndex);
		query.setIsShowList(isShowList);
		query.setIsTop(isTop);
		query.setOrder(1);
		query.setStartTime(startTime);
		query.setEndTime(endTime);
		
	    int curPage = WebUtils.getRequestParameterAsInt(request, "curPage", 1);
	    PageBean pageBean = new PageBean();
	    pageBean.setCurPage(curPage);
	    List<PerformanceInfo> performanceList = performanceDao.getPerformanceListByCondition(query, pageBean.getStart(), pageBean.getRowsPerPage());
	    pageBean.setMaxRowCount(performanceDao.getPerformanceCountByCondition(query));
	    pageBean.setMaxPage();
	    pageBean.setPageNoList();
	    
		modelMap.put("cateList", cateList);
		modelMap.put("performanceList", performanceList);
		modelMap.put("query", query);
		modelMap.put("pageBean", pageBean);
		mav.setViewName("super/performance/performance-list");
		mav.addAllObjects(modelMap);
		return mav ;
	}
	
	
	private static boolean isNumeric(String [] array){
		if(array == null || array.length == 0 ){
			return false ;
		}
		Pattern pattern = Pattern.compile("\\d+");
		for(String str : array){
		    if(!pattern.matcher(str).matches()){
		    	return false ;
		    }
		}
		return true ;
	}
}